package gravitationally_adjustable_physical_ball;

import java.awt.*;

public class Ball {
    private double x;
    private double y;
    private static Color color;
    private static int g;
    private double velocity;
    private static int diameter;
    private boolean canMove=true;
    private static final Color DEFAULT_COLOR=new Color(181, 230, 29);
    public Ball(){
        color=DEFAULT_COLOR;
        diameter=50;
        Ball.g=10;
        this.velocity=0;
        this.x=400;
        this.y=Ground.getY()-diameter;
    }

    public void setX(double x) {
        this.x = x;
    }

    public void setY(double y) {
        this.y = y;
    }

    public static void setG(int g) {
        Ball.g = g;
    }
    public void setCanMove(boolean canMove){
        this.canMove=canMove;
    }

    public static void setColor(Color color) {
        Ball.color = color;
    }

    public static void setDiameter(int diameter) {
        Ball.diameter = diameter;
    }

    public void setVelocity(double velocity) {
        this.velocity = velocity;
    }
    public static int getG(){
        return g;
    }

    public static int getDiameter() {
        return diameter;
    }

    public double getY() {
        return y;
    }

    public double getX() {
        return x;
    }

    public double getVelocity() {
        return velocity;
    }

    public void move(int millis){
        if(this.x<0){
            this.x=0;
        }
        if(this.x>800-diameter){
            this.x=800-diameter;
        }
        if(this.y<0){
            this.y=0;
            this.velocity=-this.velocity;
        }
        if(this.y>=Ground.getY()-diameter){
            this.y=Ground.getY()-diameter;
            if(Math.abs(this.velocity)<=5){
                this.velocity=0;
            }
            else {
                this.velocity=-this.velocity/2;
            }
        }
        if(canMove) {
            this.y = this.y + ((double) millis / 1000) * 50*velocity;
            velocity = velocity + ((double) millis / 1000) * g;
        }
        else {
            velocity=0;
        }
    }
    public void paint(Graphics g){
        g.setColor(color);
        g.fillOval((int) x,(int) y,diameter,diameter);
    }
}
//小球